package com.clstu.encap;

import java.sql.SQLOutput;

public class Encapsulation {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("小明gfdgdfg");
        person.setAge(204);
        person.setSalary(10000);
        System.out.println(person.info());

        Person jack = new Person("jackerer", 220, 30000);
        System.out.println(jack.info());
    }
}

class Person{
    private String name;
    private int age;
    private int salary;
    private String job;

    public String getName() {
        return name;
    }

    public Person(String name) {
        this.name = name;
    }

    public Person() {
    }

    public Person(String name, int age, int salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;//绕过检测机制
//        setSalary(salary);
//        setAge(age);
//        setName(name);//不绕过检测机制
    }

    public void setName(String name) {
        if(name.length()>=2&&name.length()<=6)
        this.name = name;
        else {
            System.out.println("名字的长度不对（2-6），默认为：王富贵");
            this.name="王富贵";
        }
    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {
        if(age>=0&&age<=120)
        this.age = age;
        else{
            System.out.println("年龄范围不对，默认设置年龄为18");
            this.age=18;
        }
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public String info(){
        return "name="+name+"  age="+age+"  salary="+salary ;
    }
}